﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test8
{
    class Program
    {
        //实验要求：定义磁盘类Disk及其派生类HardDisk(硬盘)、Flash(闪盘)和CDROM(光盘)，在其中定义记录磁盘容量的字段，并通过虚拟方法和重写方法来模拟对磁盘内容的写入和删除
        //分析：根据题目要求，使用控制台程序设计实现类结构，父类(基类)中的写入和删除是虚方法 virtual，派生类中的是重写（重载）override方法。
        class Disk
        {
            internal List<int> diskCount = new List<int>();//磁盘内容

            public Disk()
            {
                Console.WriteLine("磁盘加载成功");
            }
             public int GetDiskSize()
            {
                return diskCount.Count();
            }

            public void GetDiskCapacity()
             {
                 Console.WriteLine("该磁盘容量是动态增长的，具有无限的容量");
             }
            virtual public void AddDisk(int count)
             {
                 Console.WriteLine("磁盘正在写入中");
                 diskCount.Add(count);
                 Console.WriteLine("写入磁盘成功");
             }
            virtual public void RemoveDisk(int index)
             {
                 if (index < 0 || index > GetDiskSize())
                 {
                     Console.WriteLine("非法删除磁盘内容");
                     return;
                 }
                 else
                 {
                     Console.WriteLine("正在进行磁盘内容删除");
                     diskCount.RemoveAt(index);
                     Console.WriteLine("磁盘内容删除成功");
                 }
                 
             }
        }

        class HardDisk : Disk
        {
            public HardDisk()
            {
                Console.WriteLine("硬盘加载成功");
            }

            public override void AddDisk(int count)
            {
                Console.WriteLine("硬盘正在写入中");
                diskCount.Add(count);
                Console.WriteLine("写入硬盘成功");
            }

            public override void RemoveDisk(int index = 0)
            {
                if (index < 0 || index > GetDiskSize())
                {
                    Console.WriteLine("非法删除硬盘内容");
                    return;
                }
                Console.WriteLine("正在进行硬盘内容删除");
                diskCount.RemoveAt(index);
                Console.WriteLine("硬盘内容删除成功");
            }
        }

        class Flash : Disk
        {
            public Flash()
            {
                Console.WriteLine("闪盘加载成功");
            }

            public override void AddDisk(int count)
            {
                Console.WriteLine("闪盘正在写入中");
                diskCount.Add(count);
                Console.WriteLine("写入闪盘成功");
            }

            public override void RemoveDisk(int index = 0)
            {
                if (index < 0 || index > GetDiskSize())
                {
                    Console.WriteLine("非法删除闪盘内容");
                    return;
                }
                Console.WriteLine("正在进行闪盘内容删除");
                diskCount.RemoveAt(index);
                Console.WriteLine("闪盘内容删除成功");
            }

        }

        class CDROM : Disk
        {
            public CDROM()
            {
                Console.WriteLine("光盘加载成功");
            }

            public override void AddDisk(int count)
            {
                Console.WriteLine("光盘正在写入中");
                diskCount.Add(count);
                Console.WriteLine("写入光盘成功");
            }

            public override void RemoveDisk(int index = 0)
            {
                if (index < 0 || index >= GetDiskSize())
                {
                    Console.WriteLine("非法删除光盘内容");
                    return;
                }
                Console.WriteLine("正在进行光盘内容删除");
                diskCount.RemoveAt(index);
                Console.WriteLine("光盘内容删除成功");
            }
        }


        static private void testDisk()
        {
            Disk disk = new Disk();
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());
            for (int i = 0; i < 10; i++)
            {
                disk.AddDisk(i);
            }
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());

            //故意设置为-1检查磁盘边界处理
            for (int i = 9; i >= -1; i--)
            {
                disk.RemoveDisk(i);
            }
        }
        static private void testHardDisk()
        {
            HardDisk disk = new HardDisk();
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());
            for (int i = 0; i < 10; i++)
            {
                disk.AddDisk(i);
            }
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());

            //故意设置为-1检查磁盘边界处理
            for (int i = 9; i >= -1; i--)
            {
                disk.RemoveDisk(i);
            }
        }

        static private void testFlash()
        {
            Flash disk = new Flash();
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());
            for (int i = 0; i < 10; i++)
            {
                disk.AddDisk(i);
            }
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());

            //故意设置为-1检查磁盘边界处理
            for (int i = 9; i >= -1; i--)
            {
                disk.RemoveDisk(i);
            }
        }

        static private void testCDROM()
        {
            CDROM disk = new CDROM();
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());
            for (int i = 0; i < 10; i++)
            {
                disk.AddDisk(i);
            }
            // Console.WriteLine("获取磁盘容量" + disk.GetDiskCapacity());
            Console.WriteLine("当前已用容量" + disk.GetDiskSize());

            //故意设置为-1检查磁盘边界处理
            for (int i = 9; i >= -1; i--)
            {
                disk.RemoveDisk(i);
            }
        }
        
        static void Main(string[] args)
        {
            //testDisk();
            //testHardDisk();
            //testFlash();
            testCDROM();
        }
    }
}
